/**
 * 
 * merge level=80
 * 
 * 主菜单控制器，主菜单可以是菜单条和菜单树
 */
Ext.define('Jfok.controller.MenuRegion', {
	extend : 'Ext.app.Controller',
	views : ['Jfok.view.region.MainMenuToolbar', 'Jfok.view.region.MainMenuTree'],

	init : function() {
		this.control({

					'mainmenutoolbar menu menuitem' : {
						click : this.menuitemClick
					},

					'mainmenutree' : {
						cellclick : this.cellClick
					},

					'mainmenutoolbar tool#left ' : {
						click : this.showMenuInTreePanel
					},

					'mainmenuregion tool#up' : {
						click : this.showMenuInToolbar
					},

					// 主页里面的导航按钮的事件
					'functionnavigatepanel button' : {
						click : this.menuitemClick
					},

					'mainmenuregion label[menuLabel]' : {
						
							click : this.menuLabelClick
						
					}

				})
	},

	// 菜单 label 点击
	menuLabelClick : function(item) {
		item.addCls("selected");
		this.menuitemClick(item);
	},

	menuitemClick : function(item) {
		 debugger;
		var mainregion = this.getApplication().getController('MainRegion');
		if (item.moduleName) {
			if (item.parentFilter) {
				var p = 'var fp ' + item.parentFilter;
				eval(p);
				mainregion.addFilterModule(item.moduleName, fp);
			} else
				mainregion.addModuleToMainRegion(item.moduleName);
		} else if (item.report) {
			mainregion.addReportToMainRegion({
						reportGroupId : item.reportGroupId,
						text : item.text,
						icon : item.icon
					});
		}
	},

	cellClick : function(cell, td, cellIndex, record) {
		debugger;
		var mainregion = this.getApplication().getController('MainRegion');
		if (record.raw.report) {
			mainregion.addReportToMainRegion({
						reportGroupId : record.raw.reportGroupId,
						text : record.data.text,
						icon : record.data.icon
					});
		} else if (record.raw.parentFilter) {
			var p = 'var fp ' + record.raw.parentFilter;
			eval(p);
			mainregion.addFilterModule(record.raw.moduleName, fp);
		} else
			mainregion.addModuleToMainRegion(record.raw.moduleName);
	},

	showMenuInToolbar : function(button) {
		button.up('viewport').down('mainmenuregion').setVisible(false);
		button.up('viewport').down('mainmenutoolbar').setVisible(true);
		Cookies.set('menutoolbar', 'true');
	},

	showMenuInTreePanel : function(button) {
		button.up('viewport').down('mainmenuregion').setVisible(true);
		button.up('viewport').down('mainmenutoolbar').setVisible(false);
		Cookies.set('menutoolbar', 'false');
	}

})